package com.soufang.ui;

import com.baidu.mapapi.SDKInitializer;
import com.soufang.ui.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Toast;

import com.viewpagerindicator.TabPageIndicator;

/**
 * @brief 欢迎页面后进入的主页面
 * @author zxx
 *
 */
public class MainActivity  extends FragmentActivity {
		
	private static final String[] TITLE = new String[] { "首页", "上拉下拉DEMO", "gps获取经纬度", "更多..."};
	private static final int homeKey = 0;
	private static final int google_pullctrlkey =1;
	private static final int gps_page = 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.activity_main);
		
		FragmentPagerAdapter adapter = new PageIndicatorAdapter(getSupportFragmentManager());
        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);

      
        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);
        
       
        indicator.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				Toast.makeText(getApplicationContext(), TITLE[arg0], Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
        
	}
	
	
    class PageIndicatorAdapter extends FragmentPagerAdapter {
        public PageIndicatorAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        	
        	Fragment fragment = null;
        	switch(position)
        	{
        		case homeKey:
        		{
        			fragment = new homeFragment();
        			break;
        		}
        		case google_pullctrlkey:{
        			fragment = new googlePullFragment();
        			break;
        		}
        		case gps_page:{
        			fragment = new gpsFragment();
        			break;
        		}
        		default:
        		{
                	fragment = new ItemFragment();  
                    Bundle args = new Bundle();  
                    args.putString("arg", TITLE[position]);  
                    fragment.setArguments(args);  
        		}
        		
        	}

            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position % TITLE.length];
        }

        @Override
        public int getCount() {
            return TITLE.length;
        }
    }

}
	

